#!/usr/bin/python3
#-*-coding:utf-8-*-

###############################################################
## Name       : gen_reg
## Author     : gaojiaming
## Date       : Tue Sep 20 16:39:36 CST 2022
## Description:
##
##
###############################################################

import sys
import os
import re
import argparse
import subprocess
import time

def input_args_proc():#{{{
    global top
    global xml
    global ralf
    global ral_sv
    global rtl_sv
    parser = argparse.ArgumentParser(description="argparse info")
    parser.add_argument('-o', action='store_true', default=False, help='open this script')
    parser.add_argument('-t', default="", help='top_module')
    parser.add_argument('-x', help='cfg.xml')
    result = parser.parse_args()
    if result.o == True:
        os.system("gvim %s" % __file__)
        sys.exit(0)
    top = result.t
    xml = result.x
pass#}}}

def pre_main_proc():#{{{
    global sys
    global top
    global xml
    global xml_dict
    global ralf
    global ral_sv
    global rtl_sv
    global sys_flag
    global bt_list

    sys_flag = 0
    bt_list = []
    with open(xml, "r")as hd:
        handle = hd.readlines()
        for line in handle:
            re0 = re.match(r"      <spirit:name>(\w+)</spirit:name>", line)
            re1 = re.match(r"        <spirit:name>(\w+)</spirit:name>", line)
            if re0:
                sys = re0.group(1)#like SPU_CFG
            elif re1:
                bt_list.append(re1.group(1))#like IPG_CFG
    if top == "":
        i = 0
        for bt in bt_list:
            print("    {0}.{1}".format(i,bt))
            i = i+1
        print("    {0}.SYS({1}: All Block)".format(i, sys))
        inp = input("You sel: ")
        if int(inp) == i:
            top = sys
            sys_flag = 1
        else:
            top = bt_list[int(inp)]
    
    ralf = re.sub(r"xml$", "ralf", xml)
    xml_dict = re.sub(r"\.xml$", "", xml)
    ral_sv = "ral_{0}.sv".format(top)
    rtl_sv = "ral_blk_{0}_{1}_rtl.sv".format(sys, top)

    return sys_flag
pass#}}}

def blk_main_proc():#{{{
    os.system("ralgen -full64  -ipxact2ralf {0}".format(xml))
    print("Gen ralf at ./{0}".format(ralf))
    
    os.system("mkdir -p reg/{3}; ralgen -full64 -t {0} -I reg -uvm {1} > log; rm log; mv {2} ./reg/{3}/".format(top, ralf, ral_sv, xml_dict))
    print("Gen ral at ./reg/{0}/{1}".format(xml_dict, ral_sv))

    os.system("ralgen -full64 -t {0} -R reg/{2}/rtl_{0} {1} > log; rm log".format(top, ralf, xml_dict))
    print("Gen rtl at ./reg/{2}/rtl_{0}/{1}".format(top, rtl_sv, xml_dict))

pass#}}}

def sys_main_proc():#{{{
    os.system("ralgen -full64  -ipxact2ralf {0}".format(xml))
    os.system("mkdir -p reg/{0};".format(xml_dict))
    
    os.system("ralgen -full64 -t {0} -I reg -uvm {1} > log; rm log; mv {2} ./reg/{3}/".format(top, ralf, ral_sv, xml_dict))
    for blk in bt_list:
        blk_sv = "ral_{0}.sv".format(blk)
        os.system("ralgen -full64 -t {0} -I reg -uvm {1} > log; rm log; mv {2} ./reg/{3}/".format(blk, ralf, blk_sv, xml_dict))
    
    os.system("ralgen -full64 -t {0} -R reg/{1}/ {2} > log; rm log".format(top, xml_dict, ralf))
    for blk in bt_list:
        blk_rtl = r"ral_blk_{0}_{1}_rtl.sv".format(top, blk)
        reg_rtl = r"ral_reg_{0}_{1}_".format(top, blk)
        blk_dict= r"rtl_{0}".format(blk)
        os.system("mkdir -p ./reg/{0}/{1}".format(xml_dict, blk_dict))
        os.system("mv ./reg/{0}/{1} ./reg/{0}/{2} -f".format(xml_dict, blk_rtl, blk_dict))
        os.system("mv ./reg/{0}/{1}* ./reg/{0}/{2} -f".format(xml_dict, reg_rtl, blk_dict))

    print("Gen ralf at ./{0}".format(ralf))
    print("Gen ral at ./reg/{0}/{1}".format(xml_dict, ral_sv))
pass#}}}

def main():
    input_args_proc()
    sys_flag = pre_main_proc()
    if sys_flag == 1:
        sys_main_proc()
    else:
        blk_main_proc()

if __name__ == "__main__":
    print("%s is working" % __file__)
    print("Please source /home/global63/EDA/eda.bash")
    main()
